package barinadroid.game;

import java.util.ArrayList;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl.IOnScreenControlListener;
import org.andengine.engine.camera.hud.controls.DigitalOnScreenControl;
import org.andengine.entity.scene.IOnAreaTouchListener;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.ButtonSprite.OnClickListener;
import org.andengine.input.touch.TouchEvent;
import android.opengl.GLES20;

public class ControlsScene/* extends HUD */implements IOnScreenControlListener, OnClickListener
{
	public static ControlsScene instance;

	public static ControlsScene getSharedInstance()
	{
		if(instance == null)
			instance = new ControlsScene();
		return instance;
	}

	private GameActivity gameActivity;
	public DigitalOnScreenControl mDigitalOnScreenControl;
	public ButtonSprite buttonA, buttonB;
	private ArrayList<IOnScreenControlListener> onScreenControlListeners;
	private ArrayList<OnClickListener> buttonAClickListeners, buttonBClickListeners;
	private ArrayList<IOnAreaTouchListener> buttonATouchListeners, buttonBTouchListeners;

	private ControlsScene()
	{
		// setBackgroundEnabled(false);
		onScreenControlListeners = new ArrayList<BaseOnScreenControl.IOnScreenControlListener>();
		buttonAClickListeners = new ArrayList<ButtonSprite.OnClickListener>();
		buttonBClickListeners = new ArrayList<ButtonSprite.OnClickListener>();
		buttonATouchListeners = new ArrayList<IOnAreaTouchListener>();
		buttonBTouchListeners = new ArrayList<IOnAreaTouchListener>();
		gameActivity = GameActivity.getSharedInstance();
		mDigitalOnScreenControl = new DigitalOnScreenControl(0, gameActivity.CAMERA_HEIGHT - gameActivity.mOnScreenControlBaseTextureRegion.getHeight(), gameActivity.camera,
				gameActivity.mOnScreenControlBaseTextureRegion, gameActivity.mOnScreenControlKnobTextureRegion, 0.1f, gameActivity.getVertexBufferObjectManager(), this);
		mDigitalOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_BLEND_DST_ALPHA);
		buttonA = new ButtonSprite(gameActivity.CAMERA_WIDTH - gameActivity.mOnScreenButtonATextureRegion.getWidth(), gameActivity.CAMERA_HEIGHT
				- gameActivity.mOnScreenButtonATextureRegion.getHeight() * 2, gameActivity.mOnScreenButtonATextureRegion, gameActivity.mOnScreenButtonAPressedTextureRegion,
				gameActivity.getVertexBufferObjectManager(), this)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
			{
				for(IOnAreaTouchListener touchListener : buttonATouchListeners)
					if(touchListener.onAreaTouched(pSceneTouchEvent, buttonA, pTouchAreaLocalX, pTouchAreaLocalY))
						return true;
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		buttonB = new ButtonSprite(gameActivity.CAMERA_WIDTH - gameActivity.mOnScreenButtonBTextureRegion.getWidth() * 1.5f, gameActivity.CAMERA_HEIGHT
				- gameActivity.mOnScreenButtonBTextureRegion.getHeight(), gameActivity.mOnScreenButtonBTextureRegion, gameActivity.mOnScreenButtonBPressedTextureRegion,
				gameActivity.getVertexBufferObjectManager(), this)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
			{
				for(IOnAreaTouchListener touchListener : buttonBTouchListeners)
					if(touchListener.onAreaTouched(pSceneTouchEvent, buttonB, pTouchAreaLocalX, pTouchAreaLocalY))
						return true;
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		mDigitalOnScreenControl.attachChild(buttonA);
		mDigitalOnScreenControl.attachChild(buttonB);
		mDigitalOnScreenControl.registerTouchArea(buttonA);
		mDigitalOnScreenControl.registerTouchArea(buttonB);
		mDigitalOnScreenControl.refreshControlKnobPosition();
	}

	@Override
	public void onControlChange(BaseOnScreenControl arg0, float arg1, float arg2)
	{
		for(IOnScreenControlListener listener : onScreenControlListeners)
			listener.onControlChange(arg0, arg1, arg2);
	}

	@Override
	public void onClick(ButtonSprite button, float arg1, float arg2)
	{
		if(button == buttonA)
		{
			for(OnClickListener listener : buttonAClickListeners)
				listener.onClick(button, arg1, arg2);
		}
		else
			if(button == buttonB)
			{
				for(OnClickListener listener : buttonBClickListeners)
					listener.onClick(button, arg1, arg2);
			}
	}

	public void registerButtonAClickListener(OnClickListener listener)
	{
		if(listener != null)
			buttonAClickListeners.add(listener);
	}

	public void unRegisterButtonAClickListener(OnClickListener listener)
	{
		if(listener != null && buttonAClickListeners.contains(listener))
			buttonAClickListeners.remove(listener);
	}

	public void registerButtonBClickListener(OnClickListener listener)
	{
		if(listener != null)
			buttonBClickListeners.add(listener);
	}

	public void unRegisterButtonBClickListener(OnClickListener listener)
	{
		if(listener != null && buttonBClickListeners.contains(listener))
			buttonBClickListeners.remove(listener);
	}

	public void registerButtonATouchListeners(IOnAreaTouchListener touchListener)
	{
		if(touchListener != null)
			buttonATouchListeners.add(touchListener);
	}

	public void unRegisterButtonATouchListeners(IOnAreaTouchListener touchListener)
	{
		if(touchListener != null && buttonATouchListeners.contains(touchListener))
			buttonATouchListeners.remove(touchListener);
	}

	public void registerButtonBTouchListeners(IOnAreaTouchListener touchListener)
	{
		if(touchListener != null)
			buttonBTouchListeners.add(touchListener);
	}

	public void unRegisterButtonBTouchListeners(IOnAreaTouchListener touchListener)
	{
		if(touchListener != null && buttonATouchListeners.contains(touchListener))
			buttonBTouchListeners.remove(touchListener);
	}

	public void registerOnScreenControlChangeListener(IOnScreenControlListener listener)
	{
		if(listener != null)
			onScreenControlListeners.add(listener);
	}

	public void unRegisterOnScreenControlChangeListener(IOnScreenControlListener listener)
	{
		if(listener != null && onScreenControlListeners.contains(listener))
			onScreenControlListeners.remove(listener);
	}
}